__author__ = """Zach Seifts <zach.seifts@gmail.com>"""
__docformat__ = 'plaintext'

from Products.CMFCore.utils import getToolByName
from Products.CMFCore.WorkflowTool import addWorkflowFactory
from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
from Products.ExternalMethod.ExternalMethod import ExternalMethod
from Products.synchronicity.config import *

productname = 'synchronicity'

def setupProjectWorkFlow(self, workflow):
    """Define the ProjectWorkFlow workflow.
    """

    workflow.setProperties(title='ProjectWorkFlow')

    for s in ['Scheduled', 'Unscheduled', 'In Progress', 'Complete', 'Rejected', 'Requested']:
        workflow.states.addState(s)

    for t in ['Reopen', 'Resubmit', 'Complete', 'Reject', 'In Progress', 'Schedule', 'Approve']:
        workflow.transitions.addTransition(t)

    for v in ['review_history', 'comments', 'time', 'actor', 'action']:
        workflow.variables.addVariable(v)


    for l in []:
        if not l in workflow.worklists.objectValues():
            workflow.worklists.addWorklist(l)

    ## Initial State

    workflow.states.setInitialState('Requested')

    ## States initialization

    stateDef = workflow.states['Scheduled']
    stateDef.setProperties(title="""Scheduled""",
                           description="""The project is scheduled""",
                           transitions=['In Progress'])

    stateDef = workflow.states['Unscheduled']
    stateDef.setProperties(title="""Unscheduled""",
                           description="""The project is unscheduled""",
                           transitions=['Schedule'])

    stateDef = workflow.states['In Progress']
    stateDef.setProperties(title="""In Progress""",
                           description="""The project is in progress""",
                           transitions=['Complete'])

    stateDef = workflow.states['Complete']
    stateDef.setProperties(title="""Complete""",
                           description="""The project is complete""",
                           transitions=['Reopen'])

    stateDef = workflow.states['Rejected']
    stateDef.setProperties(title="""Rejected""",
                           description="""The project has been rejected""",
                           transitions=['Resubmit'])

    stateDef = workflow.states['Requested']
    stateDef.setProperties(title="""Requested""",
                           description="""The project is requested""",
                           transitions=['Approve', 'Reject'])

    ## Transitions initialization

    transitionDef = workflow.transitions['Reopen']
    transitionDef.setProperties(title="""Reopen""",
                                new_state_id="""Unscheduled""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Reopen""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Resubmit']
    transitionDef.setProperties(title="""Resubmit""",
                                new_state_id="""Requested""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Resubmit""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Complete']
    transitionDef.setProperties(title="""Complete""",
                                new_state_id="""Complete""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Complete""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Reject']
    transitionDef.setProperties(title="""Reject""",
                                new_state_id="""Rejected""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Reject""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['In Progress']
    transitionDef.setProperties(title="""In Progress""",
                                new_state_id="""In Progress""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""In Progress""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Schedule']
    transitionDef.setProperties(title="""Schedule""",
                                new_state_id="""Scheduled""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Schedule""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    transitionDef = workflow.transitions['Approve']
    transitionDef.setProperties(title="""Approve""",
                                new_state_id="""Unscheduled""",
                                trigger_type=1,
                                script_name="""""",
                                after_script_name="""""",
                                actbox_name="""Approve""",
                                actbox_url="""""",
                                actbox_category="""workflow""",
                                props={},
                                )

    ## State Variable
    workflow.variables.setStateVar('review_state')

    ## Variables initialization
    variableDef = workflow.variables['review_history']
    variableDef.setProperties(description="""Provides access to workflow history""",
                              default_value="""""",
                              default_expr="""state_change/getHistory""",
                              for_catalog=0,
                              for_status=0,
                              update_always=0,
                              props={'guard_permissions': 'Request review; Review portal content'})

    variableDef = workflow.variables['comments']
    variableDef.setProperties(description="""Comments about the last transition""",
                              default_value="""""",
                              default_expr="""python:state_change.kwargs.get('comment', '')""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['time']
    variableDef.setProperties(description="""Time of the last transition""",
                              default_value="""""",
                              default_expr="""state_change/getDateTime""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['actor']
    variableDef.setProperties(description="""The ID of the user who performed the last transition""",
                              default_value="""""",
                              default_expr="""user/getId""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)

    variableDef = workflow.variables['action']
    variableDef.setProperties(description="""The last transition""",
                              default_value="""""",
                              default_expr="""transition/getId|nothing""",
                              for_catalog=0,
                              for_status=1,
                              update_always=1,
                              props=None)


def createProjectWorkFlow(self, id):
    """Create the workflow for synchronicity.
    """

    ob = DCWorkflowDefinition(id)
    setupProjectWorkFlow(self, ob)
    return ob

addWorkflowFactory(createProjectWorkFlow,
                   id='ProjectWorkFlow',
                   title='ProjectWorkFlow')